package com.netcracker.ui.command;

import com.netcracker.util.service.ServiceFacade;
import com.netcracker.entity.Genre;
import com.netcracker.entity.Publisher;
import com.netcracker.entity.Language;
import com.netcracker.entity.Author;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class NewBookCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NewBookCommand.class);
    private final ServiceFacade serviceFacade;
    
    public NewBookCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        List<Genre> genres = null;
        try {
            genres = serviceFacade.getGenreService().getAllGenres();
        } catch (DataTransmittingException ex) {
            request.setAttribute("error", "Ошибка обращения к БД.");
            LOGGER.error("DB error: ", ex);
        } catch (NullDataException ex) {
            
        }
        request.setAttribute("genres", genres);
        
        List<Publisher> publishers = null;
        try {
            publishers = serviceFacade.getPublisherService().findAllPublishers();
        } catch (DataTransmittingException ex) {
            request.setAttribute("error", "Ошибка обращения к БД.");
            LOGGER.error("DB error: ", ex);
        } catch (NullDataException ex) {
            
        }
        request.setAttribute("publishers", publishers);
        
        List<Language> languages = null;
        try {
            languages = serviceFacade.getLanguageService().getAllLanguages();
        } catch (DataTransmittingException ex) {
            request.setAttribute("error", "Ошибка обращения к БД.");
            LOGGER.error("DB error: ", ex);
        } catch (NullDataException ex) {

        }
        request.setAttribute("languages", languages);
        
        List<Author> authors = null;
        try {
            authors = serviceFacade.getAuthorService().getAllAuthors();
        } catch (DataTransmittingException ex) {
            request.setAttribute("error", "Ошибка обращения к БД.");
            LOGGER.error("DB error: ", ex);
        } catch (NullDataException ex) {
        }
        request.setAttribute("authors", authors);
        
        return "AddBook.jsp";
    }
    
}
